package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorFeedback;
import guoanmaker.operator.business.model.PersonalUser;
import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.modelnobind.OperatorFeedbackMove;
import guoanmaker.operator.business.repository.OperatorFeedbackRepository;
import guoanmaker.operator.business.repository.ProviderDictionaryRepository;

@Service
public class OperatorFeedbackService implements OperatorFeedbackServiceInterface {

	@Autowired
	private OperatorFeedbackRepository operatorFeedbackRepository;
	
	@Autowired
	private OperatorSensitiveWordServiceInterface peratorSensitiveWordService;
	
	@Autowired
	private ProviderDictionaryRepository providerDictionaryRepository;
	
	@PersistenceContext
	private EntityManager manager;
	
	public synchronized Map<String,Object> insertOperatorFeedback(String userId, String feedbackContext) {
		Map<String,Object> map=new HashMap<String,Object>();
		PerviderDictionary perviderDictionary=providerDictionaryRepository.getOne("1000");
		String data=perviderDictionary.getDictValue(); //日   周   月  年 
		Long frequency=Long.parseLong(perviderDictionary.getDictName());
		String jpql="select count(0) from OperatorFeedback o where o.personalUser.id=:userId and ";
		if(data.equals("日")){
			jpql+=" to_days(o.feedbackDatetime) = to_days(now())";
		}
		else if(data.equals("周")){
			jpql+=" YEARWEEK(date_format(o.feedbackDatetime,'%Y-%m-%d')) = YEARWEEK(now())";
		}
		else if(data.equals("月")){
			jpql+=" date_format(o.feedbackDatetime,'%Y-%m')=date_format(now(),'%Y-%m')";
		}
		else if(data.equals("年")){
			jpql+=" YEAR(o.feedbackDatetime) = YEAR( NOW( ) ) ";
		}
		else{
			Set<String> set=peratorSensitiveWordService.sensitiveWordVerification(feedbackContext);
			if(set!=null&&set.size()>0){
				map.put("stauts", -1);
				map.put("set", set);
				return map;
			}
			PersonalUser personalUser=new PersonalUser();
			personalUser.setId(userId);
			OperatorFeedback operatorFeedback=new OperatorFeedback();
			operatorFeedback.setPersonalUser(personalUser);
			operatorFeedback.setFeedbackContext(feedbackContext);
			operatorFeedback.setFeedbackDatetime(new Date());
			operatorFeedback=operatorFeedbackRepository.save(operatorFeedback);
			map.put("stauts", 1);
			map.put("operatorFeedback", operatorFeedback);
			return map;
		}
		Query query=manager.createQuery(jpql).setParameter("userId", userId);
		Long Long=(Long)query.getSingleResult();
		if(Long>=frequency){
			map.put("stauts", 2);//反馈次数已经用完
		}
		else{
			Set<String> set=peratorSensitiveWordService.sensitiveWordVerification(feedbackContext);
			if(set!=null&&set.size()>0){
				map.put("stauts", -1);
				map.put("set", set);
				return map;
			}
			PersonalUser personalUser=new PersonalUser();
			personalUser.setId(userId);
			OperatorFeedback operatorFeedback=new OperatorFeedback();
			operatorFeedback.setPersonalUser(personalUser);
			operatorFeedback.setFeedbackContext(feedbackContext);
			operatorFeedback.setFeedbackDatetime(new Date());
			operatorFeedback=operatorFeedbackRepository.save(operatorFeedback);
			map.put("stauts", 1);
			map.put("operatorFeedback", operatorFeedback);
		}
		return map;
	}
	
	public Map<String, Object> selectOperatorFeedbackPage(Integer page, Integer size) {
		Map<String, Object> map=new HashMap<String, Object>();
		String  jpql="select new guoanmaker.operator.business.modelnobind.OperatorFeedbackMove(u.username,o.feedbackDatetime,"
				+ "o.feedbackContext) from OperatorFeedback o left join o.personalUser u order by o.feedbackDatetime desc";
		Query query=manager.createQuery(jpql);
		int count=query.getResultList().size();
		query.setMaxResults(size).setFirstResult(page*size);
		List<OperatorFeedbackMove> list=query.getResultList();
		map.put("count", count);
		map.put("list", list);
		return map;
	}
	
	@Transactional
	public Integer FeedbackSetting(String date, String frequency) {//dictValue   表示 日  周   月  年    dictName 表示  次数
		/*PerviderDictionary perviderDictionary=providerDictionaryRepository.getOne("1000");
		Integer data=perviderDictionary.getDictValue();
		Integer count=perviderDictionary.getDictValue();*/
		String jpql="update PerviderDictionary p set p.dictValue=:dictValue,p.dictName=:dictName where p.id=1000";
		Query query=manager.createQuery(jpql).setParameter("dictValue", date).setParameter("dictName", frequency);
		return query.executeUpdate();
	}

	@Override
	public PerviderDictionary selectFeedbackSetting() {
		return providerDictionaryRepository.findOne("1000");
	}

}
